<?php


class Model_Sesiones extends Zend_Db_Table_Abstract  {

    protected $_name = 'tbl_administracion_sesiones';

    public function GetTblName() {
        return $this->_name;
    }
    
    public function GetSesionById($SesionId) {
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $this->select();
        $select->from(array($this->_name));
        $select->where('sesiones_id = "'.$SesionId.'"');
        $select->setIntegrityCheck(false);
        $query = $db->query($select);
        return $query->fetchAll();
    }
    
    public function GetSesiones(){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $this->select();
        $select->from(array($this->_name));
        $select->setIntegrityCheck(false);
        $query = $db->query($select);
        return $query->fetchAll();
    }

    public function Guardar($sesion) {
        $arr_sesion = $sesion->ToArray();
        
        $existe = $this->GetSesionById($arr_sesion['sesiones_id']);
        if (count($existe) > 0) {$this->Eliminar($arr_sesion['sesiones_id']);}

        $db = Zend_Db_Table::getDefaultAdapter();
        $query = "INSERT INTO `".$this->_name;

        $query .= "` (";
        foreach ($arr_sesion as $attr => $val ) {
            $query .= "`".$attr."`, ";
        }
        $query = substr($query, 0, -2);
        $query .= ") VALUES (";
        foreach ($arr_sesion as $attr => $val ) {
            $query .= "'".$val."', ";
        }
        $query = substr($query, 0, -2);
        $query .= ");";
        
        $db->query($query);
    }
    
    public function Eliminar($sesionId) {
        $db = Zend_Db_Table::getDefaultAdapter();
        $query = "DELETE FROM ".$this->_name." WHERE ";
        $query .= "`sesiones_id` = '".$sesionId."'";
        $db->query($query);
    }
    
//    public function Limpiar($tiempo = 1800) {
//        
//        var_dump(date('d-m-Y H:i:s'));
//        $ahora = strtotime(date("d-m-Y H:i:s"));
//        
//        $caduca = $ahora - $tiempo;
//        var_dump(date('d-m-Y H:i:s', $caduca));
//    }
    
}
